require 'ostruct'
include ApplicationHelper
include WishListsHelper

class MaterialsController < ApplicationController  
  before_filter :load_session

  public

  def redirect_back
    redirect_back_or_default params[:url]
  end

  # GET /materials
  # GET /materials.xml
  def index
    save_redirect_back request.request_uri
    search
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @materials }
    end
  end

  # GET /materials/1
  # GET /materials/1.xml
  def show
    @material = Material.find(params[:id])
    @wish_lists = load_wish_list_current_user
    render :action => "material_info_for_details"
  end

  def show_selector
    @material = Material.find(params[:id])
    respond_to do |format|
      format.html # show_selector.html.erb
      format.xml  { render :xml => @material }
    end
  end

  # GET /materials/new
  # GET /materials/new.xml
  def new
    @material = Material.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @material }
    end
  end

  # GET /materials/1/edit
  def edit
    @material = Material.find(params[:id])
  end

  # POST /materials
  # POST /materials.xml
  def create
    @material = Material.new(params[:material])

    respond_to do |format|
      if @material.save
        flash[:notice] = t('msg_record_successfully_created')
        format.html { redirect_to(@material) }
        format.xml  { render :xml => @material, :status => :created, :location => @material }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @material.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /materials/1
  # PUT /materials/1.xml
  def update
    @material = Material.find(params[:id])

    respond_to do |format|
      if @material.update_attributes(params[:material])
        flash[:notice] =  t('msg_record_successfully_updated')
        format.html { redirect_to(@material) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @material.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /materials/1
  # DELETE /materials/1.xml
  def destroy
    @material = Material.find(params[:id])
    @material.destroy

    respond_to do |format|
      format.html { redirect_to(materials_url) }
      format.xml  { head :ok }
    end
  end

  #cargo los valores por defecto de las colecciones
  def load_defect_collection
    @categories = Category.find(:all, :conditions => "category_id is null")
    @materials = nil
  end

  def save_wish_list
    error = false

    if !params[:wish_list_selected].nil? && params[:wish_list_selected] != "-1"
      wish_list_id = params[:wish_list_selected].to_i
    else
      flash[:error] = t('lbl_select_wish_list')
      error = true
    end

    if !error
      if params[:material_selected].nil?
        flash[:error] = t('lbl_must_select_materials')
        error = true
      end
    end

    if !error
      wish_list = WishList.find wish_list_id
      if wish_list.type_list == WishList::TYPE_CART
        #si la lista es el carro de compras
        session[:material_selected] = params[:material_selected].to_i
        redirect_to my_wish_list_define_materials_to_cart_from_material_details_path
      else
        #sino lo trato como una lista comun
        materials_id = []
        materials_id << params[:material_selected].to_i
        process_with_error = WishListsHelper.add_materials_to_wish_list(wish_list_id, materials_id)
        if process_with_error
          flash[:error] = t('lbl_material_add_wish_list_error')
        else
          flash[:notice] = t('lbl_material_add_wish_list_ok')
        end
      end
    end
    if error || wish_list.type_list != WishList::TYPE_CART
      @material = Material.find(:first, materials_id)
      @wish_lists = load_wish_list_current_user
      render :action => "material_info_for_details"
    end
  end

  def selector_materials_list
    search
    render :partial=>'materials/selector_materials_list' , :locals => {:search_title => params[:search_title], :search_isbn => params[:search_isbn], :search_issn => params[:search_issn]}, :layout=>false
  end

  def selector_materials_list_from_paginate
    search
  end

  def selector
    save_redirect_back request.request_uri
    search
    respond_to do |format|
      format.html # selector.html.erb
      format.xml  { render :xml => @materials }
    end
  end

  
  private

  def load_session
    #siempre se cargan los combos
    @currencies = Currency.find(:all, :order=>:name)
    @material_types =   MaterialType.find(:all, :order=>:description)
  end

  def search
    
    #se obtienen los valores de los filtros
    record_search = OpenStruct.new
    record_search.title = params[:search_title] if !params[:search_title].nil? and params[:search_title].size > 0
    record_search.isbn = params[:search_isbn] if !params[:search_isbn].nil? and params[:search_isbn].size > 0
    record_search.page = params[:page]

    if !params[:material].nil? and !params[:material][:material_type].nil?
        @search_material_type = params[:material][:material_type]
    else
        @search_material_type = -1
    end

    if @search_material_type != -1
      record_search.material_type_id = @search_material_type
    end
    
    #se realiza la consulta sobre el modelo (motor de busquedas)
    @materials = Material.search_engine record_search
  end

end

